﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Disappearwind.PortalSolution.PortalWeb.Models;

namespace Disappearwind.PortalSolution.PortalWeb.Business
{
    /// <summary>
    /// User info manager
    /// </summary>
    public class UserInfoBusiness : BaseBusiness
    {
        /// <summary>
        /// Return all user
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetUser()
        {
            var c = from p in DBContext.UserInfo
                    orderby p.CreateDate descending
                    select p;
            if (c != null)
            {
                return c.ToList();
            }
            else
            {
                return new List<UserInfo>();
            }
        }
        /// <summary>
        /// Add a user to db
        /// </summary>
        /// <param name="userInfo"></param>
        public void AddUser(UserInfo userInfo)
        {
            userInfo.UserId = Guid.NewGuid();
            userInfo.CreateDate = DateTime.Now;
            DBContext.AddToUserInfo(userInfo);
            DBContext.SaveChanges();
        }

        /// <summary>
        /// Judge if the userName has been used
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool IsExist(string userName)
        {
            var c = from p in DBContext.UserInfo
                    where p.UserName == userName
                    select p;
            if (c == null || c.Count() == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
